// IconButton.cpp: implementation of the IconButton class.
//
//////////////////////////////////////////////////////////////////////

#include "IconButton.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IconButton::IconButton(float x_pos, float y_pos, float x_dim, float y_dim, GLuint tn, GLuint tf, GLuint tc )
{
this->start_x = x_pos;
this->start_y = y_pos;

this->dim_x = x_dim;
this->dim_y = y_dim;

this->mouseIsOver = 0;
this->isSelected = 0;

texName[0] = tn;
texName[1] = tf;
texName[2] = tc;

this->one = false;
}

IconButton::IconButton(GLuint tn)
{
this->start_x = 0.0f;
this->start_y = 0.0f;

this->dim_x = 0.0f;
this->dim_y = 0.0f;

this->mouseIsOver = 0;
this->isSelected = 0;

texName[0] = tn;
texName[1] = tn;
texName[2] = tn;

this->one = true;
}

IconButton::IconButton(GLuint tn,GLuint tf,GLuint tc)
{
this->start_x = 0.0f;
this->start_y = 0.0f;

this->dim_x = 0.0f;
this->dim_y = 0.0f;

this->mouseIsOver = 0;
this->isSelected = 0;

texName[0] = tn;
texName[1] = tf;
texName[2] = tc;

this->one = false;
}

IconButton::~IconButton()
{
glDeleteTextures(1,&texName[0]);
glDeleteTextures(1,&texName[1]);
glDeleteTextures(1,&texName[2]);
}

void IconButton::set(float x, float y, float dim_x, float dim_y)
{
this->start_x = x;
this->start_y = y;

this->dim_x = dim_x;
this->dim_y = dim_y;

//printf("%f %f %f %f\n",start_x,start_y,dim_x,dim_y);
}

void IconButton::display()
{
  /*Set up the 2D ambient */
int fh = (float)glutGet ( (GLenum) GLUT_WINDOW_HEIGHT ) ;
int fw = (float)glutGet ( (GLenum) GLUT_WINDOW_WIDTH  ) ;

  glDisable      ( GL_LIGHTING ) ;
  glDisable      ( GL_DEPTH_TEST ) ;
  glDisable      ( GL_CULL_FACE  ) ;

  glEnable       ( GL_ALPHA_TEST ) ;
  glEnable       ( GL_BLEND ) ;
  glAlphaFunc    ( GL_GREATER, 0.1f ) ;
  glBlendFunc    ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
  
  //create an projection that acts like a 2D screen
  glMatrixMode   ( GL_PROJECTION ) ;
  glPushMatrix   () ;
  glLoadIdentity () ;
  glOrtho        ( 0.0, 1.0, 0.0, fw/fh, 0.0, 1.0 ) ;

  glMatrixMode   ( GL_MODELVIEW ) ;
  glPushMatrix   () ;
  glLoadIdentity () ;     
     
   
   /* Draw and paint map and widget's border when selected or mouse over*/
   glColor3f(1.0f,1.0f,1.0f);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   if(isSelected)
   {
   if(one) {glColor3f(1.0f,0.0f,0.0f);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);}
   glBindTexture(GL_TEXTURE_2D, texName[2]);
   } 
   else if(mouseIsOver)
   {
   if(one) {glColor3f(0.0f,1.0f,0.0f);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);}
   glBindTexture(GL_TEXTURE_2D, texName[1]);
   } 
   else
   {
	if(one) {glColor3f(1.0f,1.0f,1.0f);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);}
	glBindTexture(GL_TEXTURE_2D, texName[0]);
   }

   //printf("%f %f %f %f\n",start_x,start_y,dim_x,dim_y);

   // Draw the texture!!!
   glEnable(GL_TEXTURE_2D);
  // if(!one) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   glBegin(GL_QUADS);
   glTexCoord2f(0.0f, 1.0f); glVertex2f(start_x, start_y);
   glTexCoord2f(0.0f, 0.0f); glVertex2f(start_x, start_y + dim_y);
   glTexCoord2f(1.0f, 0.0f); glVertex2f(start_x + dim_x, start_y + dim_y);
   glTexCoord2f(1.0f, 1.0f); glVertex2f(start_x + dim_x, start_y);
   glEnd();
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   glDisable(GL_TEXTURE_2D);

 /* Back to 3d view */
	
  glMatrixMode   ( GL_PROJECTION ) ;
  glPopMatrix    () ;
  glMatrixMode   ( GL_MODELVIEW ) ;
  glPopMatrix    () ;
  
  glDisable      ( GL_ALPHA_TEST ) ;
  glDisable      ( GL_BLEND ) ;
  glAlphaFunc    ( GL_ALWAYS, 0.0 ) ;
  glBlendFunc    ( GL_ONE, GL_ZERO ) ;

  glEnable     ( GL_LIGHTING ) ;
  glEnable     ( GL_DEPTH_TEST ) ;
  glEnable      ( GL_CULL_FACE  ) ;
}

void IconButton::bindTexture()
{
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   glBindTexture(GL_TEXTURE_2D, texName[0]);
   glBindTexture(GL_TEXTURE_2D, texName[1]);
   glBindTexture(GL_TEXTURE_2D, texName[2]);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}

int IconButton::selected()
{
return isSelected;
}

int IconButton::mouse(int b, int s, int x, int y)
{
int h = glutGet ( (GLenum) GLUT_WINDOW_HEIGHT ) ;
int w = glutGet ( (GLenum) GLUT_WINDOW_WIDTH  ) ;

float xp = (float)x / (float)w;
float yp = (float)(h-y) / (float)h;

if(w > h)
     {
     xp *= 1.0;
     yp *= 1.0; 
     }
else
    {
    xp *= h/w;
    yp *= 1.0;        
    }

if( (start_x <= xp) &&
    (start_y <= yp) &&
    (xp <= (start_x + dim_x)) &&
    (yp <= (start_y + dim_y)) ) //ww and wh should change after resize!!!
    {
    this->mouseIsOver = 1;   
    if( (b == PW_LEFT_BUTTON) && (s == PW_DOWN) )
        {
        if(isSelected){isSelected = 0;} 
        else{isSelected = 1;}
        }
    } 
else
    {
    this->mouseIsOver = 0;
    }
return mouseIsOver;
}

void IconButton::change()
{
if(isSelected){isSelected = 0;} 
else{isSelected = 1;}
}